<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<% String path=request.getContextPath(); String basePath=request.getScheme()+ "://" + request.getServerName() + ":" + request.getServerPort() + path+ "/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<base href="<%=basePath%>">
<title>产品添加</title>


<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
<script src="js/jquery.validate.js" type="text/javascript" ></script>
<script src="js/jquery.divbox.js" type="text/javascript"></script>
<style type="text/css">
.input_text {
width: 250px;
}
.select_1 {
width: 250px;
}
input.fieldError {
border: 1px solid #ffb042;
background-color: #fffbf5;
}
textarea.fieldError {
border: 1px solid #ffb042;
background-color: #fffbf5;
}
label.fieldError {
line-height: 23px;
color: #ffb042;
padding-left: 6px;
}
</style>
<script type="text/javascript">
var startP=1;
var Formstatus=true;
var prodType=0;
var prodCode=0;

$(document).ready(function() {
	loadNoCarProdTypes(); //加载“产品类型” 下拉列表
	loadInsurePeriod();//加载"保险期间" 下拉列表
	var $maxDelayDay = $("#maxDelayDay");
	var $minDays = $("#minDays");
    var $maxDays = $("#maxDays");
    var $minDelayDay = $("#minDelayDay");
    var $maxDelayDay = $("#maxDelayDay");
    /* var $longestCancelDay =$("#longestCancelDay"); */
  //最短延时起保时间：不小于0的整数，且首位非0
	$minDelayDay.change(function(){
		if($minDelayDay.val()!=""){
			var minDelayDayReg = new RegExp("^(0|[1-9][0-9]*|-[1-9][0-9]*)$");
			var delayflag = minDelayDayReg.test($minDelayDay.val());
			if(!delayflag) $.message("error","最短延时起保时间必须是整数");
		}
	});
  
	//最长注销天数
	/* $longestCancelDay.change(function(){
		if($longestCancelDay.val()!=""){
			var minDelayDayReg = new RegExp("^(0|[1-9][0-9]*)$");
			var delayflag = minDelayDayReg.test($longestCancelDay.val());
			if(!delayflag) $.message("error","最长注销天数必须是大于等于0的整数");
		}
	}); */
  
  	//最长延时起保时间：不小于0的整数，且首位非0
	$maxDelayDay.change(function(){
		if($maxDelayDay.val()!=""){
			var maxDelayDayReg = new RegExp("^(0|[1-9][0-9]*)$");
			var delayflag = maxDelayDayReg.test($maxDelayDay.val());
			if(!delayflag) $.message("error","最长延时起保时间必须是不小于0的整数");
		}
	});
	//最小保险期间：大于等于1的正整数
	$minDays.change(function(){
		if($minDays.val()!=""){
			var minDayReg = new RegExp("^[1-9][0-9]*$");
			var minDayflag = minDayReg.test($minDays.val());
			if(!minDayflag) $.message("error","最小保险期间必须是不小于1的整数");
		}
	});
	//最大保险期间：大于等于1的正整数，且必须大于最小保险期间
	$maxDays.change(function(){
		if($maxDays.val()!=""){
			var maxDayReg = new RegExp("^[1-9][0-9]*$");
			var maxDayflag = maxDayReg.test($maxDays.val());
			if(!maxDayflag){
				$.message("error","最大保险期间必须是不小于1的整数");
				return;
			}
			if(parseInt($minDays.val())>parseInt($maxDays.val())){
				$.message("error","最大保险期间不能小于最小保险期间");
				return;
			}
		}
	});
	 
});


//查询产品类型
function loadNoCarProdTypes(){
	$.ajax({
		url : "noCarProduct/queryProdType",
		type : 'POST',
		async : true,
		dataType:'json',
		data:{parentCode:2},
		success:function(prodTypeList){
			if($.validate(prodTypeList))return;
			
			if(prodTypeList[0]!=undefined){		
				 $.each(prodTypeList,function(i,item){
					$("#prodType").append("<option value="+item.code+">"+item.codeName+"</option>");
				 });
			}
		}
	});
}
//查询保险期间
function loadInsurePeriod(){
	$.ajax({
		url : "noCarProduct/queryInsurePeriod",
		type : 'POST',
		async : true,
		dataType:'json',
		success:function(insurePeriodList){
			if($.validate(insurePeriodList))return;
			
			if(insurePeriodList[0]!=undefined){		
				 $.each(insurePeriodList,function(i,item){
					$("#PPP_UNIT").append("<option value="+item.code+">"+item.codeName+"</option>");
				 });
			}
		}
	});
}

//表单校验
function checkFormFormate(){
	var $isPool = $("#isPool");
	var $prodName = $("#prodName");
	//var $status = $("#status");
	var $prodType = $("#prodType");
	var $minDelayDay = $("#minDelayDay");
	var $maxDelayDay = $("#maxDelayDay");
	var $minDays = $("#minDays");
    var $maxDays = $("#maxDays");
    /* var $longestCancelDay =$("#longestCancelDay"); */
    if($.trim($prodName.val()).length==0){
    	$.message("error","产品名称不能为空");
    	$prodName.focus();
    	return false;
    }
    //产品归类
    if($prodType.val()==""){
    	$.message("error","请选择产品归类");
    	$prodType.focus();
    	return false;
    }
   if($isPool.val()=="Y"){
	   var conInsuCode  = $("#conInsuCode").val();
	   if("" == conInsuCode){
		   $.message("error","请选择共保方式");
	       return false;
	   }
   }
   
   
 	//最短延时起保时间：不小于0的整数，且首位非0
   if($.trim($minDelayDay.val()).length==0){
   	$.message("error","请输入最短延时起保时间");
   	$minDelayDay.focus();
   	return false;
   }else{
   	var minDelayDayReg = new RegExp("^(0|[1-9][0-9]*|-[1-9][0-9]*)$");
		var delayflag = minDelayDayReg.test($minDelayDay.val());
		if(!delayflag){
			$.message("error","最短延时起保时间必须是不小于0的整数");
			$minDelayDay.focus();
			return false;
		}
	}
   
  /* //产品状态
    if($status.val()==""){
    	$.message("error","请选择产品状态");
    	$status.focus();
    	return false;
    }*/
  	//最长延时起保时间：不小于0的整数，且首位非0
    if($.trim($maxDelayDay.val()).length==0){
    	/* $.message("error","请输入最长延时起保时间");
    	$maxDelayDay.focus();
    	return false; */
    }else{
    	var maxDelayDayReg = new RegExp("^(0|[1-9][0-9]*)$");
		var delayflag = maxDelayDayReg.test($maxDelayDay.val());
		if(!delayflag){
			$.message("error","最长延时起保时间必须是不小于0的整数");
			$maxDelayDay.focus();
			return false;
		}
		if(parseInt($minDelayDay.val())>parseInt($maxDelayDay.val())){
			$.message("error","最长延时起保时间不能小于最短延时起保时间");
			$maxDays.focus();
			return false;
		}
	}
  	//最短保险天数：大于等于1的正整数
    if($.trim($minDays.val()).length==0){
    	/* $.message("error","请输入最短保险天数");
    	$minDays.focus();
    	return false; */
    }else{
		var minDayReg = new RegExp("^[1-9][0-9]*$");
		var minDayflag = minDayReg.test($minDays.val());
		if(!minDayflag){
			$.message("error","最小保险期间必须是不小于1的整数");
			$minDays.focus();
			return false;
		}
	}
  //最长注销天数:只能填入大于等于0的整数，默认为0.
   /*  if($.trim($longestCancelDay.val()).length=0){
    	$.message("error","请输入最长注销天数");
    	$longestCancelDay.focus();
    	return false;
    }else{
    	//alert($longestCancelDay.val());
    	var minDelayDayReg = new RegExp("^(0|[1-9][0-9]*)$");
        var delayflag = minDelayDayReg.test($longestCancelDay.val());
        if(!delayflag){
        	$.message("error","最长注销天数必须是不小于0的整数")
        	$longestCancelDay.focus();
        	return false;
        }
    } */
  	
  	//最长保险天数：大于等于1的正整数，且必须大于最短保险天数
    if($.trim($maxDays.val()).length==0){
    	/* $.message("error","请输入最长保险天数");
    	$maxDays.focus();
    	return false; */
    }else{
		var maxDayReg = new RegExp("^[1-9][0-9]*$");
		var maxDayflag = maxDayReg.test($maxDays.val());
		if(!maxDayflag){
			$.message("error","最大保险期间必须是不小于1的整数");
			$maxDays.focus();
			return false;
		}
		if(parseInt($minDays.val())>parseInt($maxDays.val())){
			$.message("error","最大保险期间不能小于最小保险期间");
			$maxDays.focus();
			return false;
		}
	}
    //销售类型校验 2015年7月18日14:08:17
   /*  var $agentType= $("#agentType");
    if($agentType.val()==""){
  		$.message("error","请选择销售类型");
  		$agentType.focus();
  		return false;
    } */
	return true; 
}

//添加险种
function addProduct(){
	if(!checkFormFormate())return;
	var prodName = $("#prodName").val();
	var prodType = $("#prodType").val();
	var status = $("#status").val();
	var $minDelayDay = $("#minDelayDay");
	var isPool = $("#isPool").val();
	var prodDesc = $("#prodDesc").val();
	var maxDelayDay = $("#maxDelayDay").val();
	var minDays = $("#minDays").val();
	var maxDays = $("#maxDays").val();
	/* var agentType_val= $("#agentType").val(); */
	var minDelayDay_val=$minDelayDay.val();
	var conInsuCode  = $("#conInsuCode").val();
	var PPP_UNIT_val=$("#PPP_UNIT").val();
	/* var longestCancelDay_val=$("#longestCancelDay").val(); ,longestCancelDay:longestCancelDay_val*/
	var minDelayUnit = $("#minDelayUnit").val();
	var tgtType = $("#tgtType").val();
    saveNocarProd({prodName:prodName,prodType:prodType,isPool:isPool,status:status,prodDesc:prodDesc,maxDelayDay:maxDelayDay,minDays:minDays,maxDays:maxDays,/* agentType:agentType_val, */conInsuCode:conInsuCode,pppUnit:PPP_UNIT_val,minDelayDay:minDelayDay_val,minDelayUnit:minDelayUnit,tgtType:tgtType});
}

function saveNocarProd(param){
	if(!Formstatus){
		$.message("error", "正在处理中...");
		return;
	}
	Formstatus=false;
	$.openLoading();
	vreset();
	$.ajax({
		url : "noCarProduct/save",
		type : 'POST',
		async : true,
		dataType:'json',
		data:param,
		success : function(obj) {
			//表单状态
			Formstatus=true;
			$.closeLoading();
			if($.validate(obj))return;
			prodType=obj.type.prodType;
			prodCode=obj.type.prodCode;
			$.dialog({
				type: "success",//success,error
				content: "产品添加成功,是否继续配置险别",
				onCancel:function(){
					openProductList();
					return;
				},
				onOk: function() {
					openInsurance();
					return;
				}
			});
			
		}
	});
}


function bindredio(){
	$("input[name='radioTemple']").click(function(){
		$("input[name='radioTemple']").parent().parent().removeClass("table_highlighter");
		$(this).parent().parent().addClass("table_highlighter");

   	});
}

function vreset(){
	$("#prodName").val("");
	$("#prodType").val("");
	$("#status").val("");
	$("#prodDesc").val("");
	$("#maxDelayDay").val("");
	$("#minDays").val("");
	$("#minDelayDay").val("");
	$("#maxDays").val("");
	$("#tgtType").val("");
	/* $("#agentType").val(0); */
	if($("#isPool").val() == "Y"){
		$("#isPool").val("N");
		$("#conInsuCode").val("");
		hidenOrShowConInsuType();
	}else{
		$("#isPool").val("N");
	}
}
function openProductList(){
	window.location.href='<%=basePath%>noCarProduct/list';
} 
function openInsurance(){
	window.location.href='<%=basePath%>noCarProduct/prodRisk?prodType='+prodType+'&prodCode='+prodCode;
}

function hidenOrShowConInsuType(){
	var isPool = $("#isPool").val();
	if("Y" == isPool){
		 $("#prodAddTable tr:eq(4) td:eq(0)").attr("colspan", 1);
		 $("#prodAddTable tr:eq(4) td:eq(0)").after("<th>共保方式：</th><td><select name='conInsuCode'id='conInsuCode'class='select_1'style='visibility: visible;width: 250px;'><option value=''>--------请选择--------</option><option value='01'>共出</option><option value='02'>共入</option></select><span class='red'>*</span></td>");
	}else{
		$("#prodAddTable tr:eq(4) td:eq(0)").attr("colspan", 3);
		$("#prodAddTable tr:eq(4) th:gt(0)").remove();
		$("#prodAddTable tr:eq(4) td:gt(0)").remove();
		
	}
}

function hidenOrShowTgtType(){
	var prodType = $("#prodType").val();
	if("049S" == prodType){
		$(".tgtType").show();
	}else{
		$(".tgtType").hide();
	}
}
</script>
</head>
<body>
<div class="current">
           <table class="current_table_1">
              <tbody>
                <tr class="f2">
                  <td>
                    <img src="./images/current_1.jpg">
                  </td>
                  <td class="current_table_1_td">
                    <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
                      <span style=" color:#136bab; ">财险产品管理
                      <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>产品配置
                      <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>新增产品
                  </span>
                  </td>
                  <td><img src="./images/current_3.jpg"></td>
              </tr>
           </tbody>
         </table>
      </div>
	      
			<form id="inputForm" >
	        <table id = "prodAddTable" class="table_05">
	              <tbody>
	              <tr>
	                  <td colspan="4" style=" font-size:12px; font-weight:bold; text-align:left;color:#3876bf; line-height:17px; background-image:url(images/centent_1.jpg)">新增产品</td>
	              </tr>
	              <tr>
	                  <th>产品名称：</th>
	                  <td><input name="prodName" id="prodName" type="text" maxlength="50"  class="input_text"><span class="red">*</span></td>
	                  <th>产品归类：</th>
	                 <td><select name="prodType" id="prodType" class="select_1" style="visibility: visible;" onchange="hidenOrShowTgtType()"><option value="">--------请选择--------</option></select><span class="red">*</span></td>
	              </tr>
	              <tr>
	                  <th>是否共保：</th>
	                  <td><select name ="isPool" id="isPool"  class="select_1" style="visibility: visible;" onchange="hidenOrShowConInsuType()">
	                  		<option value="N">否</option>
	                  		<option value="Y">是</option>
	                  	  </select></td>
	                  <th>最长延时起保时间：</th>
	                  <td><input name="maxDelayDay" id="maxDelayDay" type="text" maxlength="50"  class="input_text"></td>
	              </tr>
	              <tr>
	                  <th>最短延时起保时间：</th>
	                  <td><input name="minDelayDay"  id="minDelayDay" type="text" maxlength="5" class="input_text"><span class="red">*</span>
	                  	<span>
	                  		<select id="minDelayUnit">
	                  			<option value="d">天</option>
	                  			<option value="h">小时</option>
	                  			<option value="m">分钟</option>
	                  		</select>
	                  	</span>
	                  	<span class="blue">支持负数</span>
	                  </td>
	                   <th>保险期间：</th>
	                  <td>
	                  	<input name="minDays" id="minDays" type="text" maxlength="50"  class="input_text" style="width:60px;">&nbsp;至&nbsp;
	                  	<input name="maxDays" id="maxDays" type="text" maxlength="50"  class="input_text" style="width:60px;">&nbsp;&nbsp;
	                  	<select name="PPP_UNIT" id="PPP_UNIT" class="select_1"  style="width:55px;visibility: visible;"></select>
	                  </td>
	              </tr>
	             <%-- <tr>
	              	 <th>最长起保后注销天数：</th>
	                  <td><input name="longestCancelDay"  id="longestCancelDay" type="text" maxlength="5" class="input_text" value="0"><span class="red">*</span></td> 
	                  <th>销售类型：</th>
	                  <td>
	                  	<select name="agentType" id="agentType" class="select_1" style="visibility: visible;width: 250px;">
			              <option value="">--------请选择--------</option>
	                  	  <c:forEach var ="agentType" items="${agentTypeList}">
                  			 <option value="${agentType.code}">${agentType.codeName}</option>
                  		  </c:forEach>
	                   </select><span class="red">*</span>
	                  </td>
	              </tr> --%>
	              <tr style="display: none" class="tgtType">
	              	<th>清单类型：</th>
	              	<td>
						<select name='tgtType'id='tgtType'class='select_1'style='visibility: visible;width: 250px;'>
		              		<option value=''>--------请选择--------</option>
			              	<c:forEach var ="tgtType"  items="${tgtTypeList}">
			              		<option value="${tgtType.code}">${tgtType.codeName}</option>
			              	</c:forEach>
			           	</select>
	              	</td>
	              </tr>
	              <tr>
	                  <th>交易系统产品描述：</th>
	                  <td colspan="3"><textarea name="prodDesc" id="prodDesc"  maxlength="50"  rows="5" class="textarea_1" style="resize:none"></textarea></td>
	              </tr>
	           </tbody>
	           </table> 
	           <div align="center">
	              <input id="sub" class="input_button_1" name=""  type="button"  value="保存" onclick="addProduct()">&nbsp;&nbsp;
	              <input class="input_button_1" name="" onclick="vreset()" type="button" value="重置">
	           </div>
	           </form>
</body>
</html>